package jmine.tec.utils.spring;

import java.io.IOException;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.ResourceEntityResolver;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * Application Context que utiliza cache para evitar importações duplicadas.
 */
public class CachingFileSystemXmlApplicationContext extends FileSystemXmlApplicationContext {

    /**
     * C'tor
     * 
     * @param configLocations String[]
     * @param parentApplicationContext {@link ApplicationContext}
     */
    public CachingFileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parentApplicationContext) {
        super(configLocations, parentApplicationContext);
    }

    /**
     * Cópia do método da classe {@link FileSystemXmlApplicationContext}. A única diferença é que utiliza
     * {@link CachingXmlBeanDefinitionReader} ao invés {@link XmlBeanDefinitionReader}. {@inheritDoc}
     */
    @Override
    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {
        XmlBeanDefinitionReader beanDefinitionReader = new CachingXmlBeanDefinitionReader(beanFactory);

        beanDefinitionReader.setResourceLoader(this);
        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

        initBeanDefinitionReader(beanDefinitionReader);
        loadBeanDefinitions(beanDefinitionReader);
    }
}
